//
//  CardViewController.m
//  Lextra Junior ueb IT
//
//  Created by Dirk Lieder on 30.05.11.
//  Copyright 2011 Phosphon. All rights reserved.
//

#import "CardViewController.h"
#import "AsyncImageView.h"
#import "getFilenameHD.h"


@implementation CardViewController


@synthesize myImageView;
@synthesize myLabel1,myLabel2;
@synthesize myHL1,myHL2;
@synthesize myButton1,myButton2;
//__NGUYEN_HUY_SON__
@synthesize navigationBar;
//___END_NGUYEN_HUY_SON__
NSDictionary *myArray;
AVAudioPlayer *audioPlayer;

//____NGUYEN_HUY_SON__
- (void)monsterSelectionChanged:(NSString*)testing {
    [myLabel1 setText:testing];
}

- (BOOL)isPadPortrait
{
    
    return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
            && (self.interfaceOrientation == UIInterfaceOrientationPortrait
                || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown));
}


- (BOOL)isPadLandscape
{
    return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
            && (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight
                || self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft));
}
- (void)viewWillAppear:(BOOL)animated {
    if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad) 
    { 
        [super viewWillAppear:animated];
        [myLabel1 setText:[[NSString alloc] initWithFormat:@"%d",[[NSUserDefaults standardUserDefaults] integerForKey:@"Integer"]]];
    }
    else
    {
        [navigationBar setTintColor:[[UIColor alloc] initWithRed:20.0 / 255 green:59.0 / 255 blue:102.0 / 255 alpha:1.0]];
        if([self isPadPortrait] == TRUE)
        {
            CGRect newFrame = CGRectMake((768 - 440)/2, 164,440 ,  440);
            myImageView.frame = newFrame;
            CGRect newFrame_lable = CGRectMake((768 - myLabel1.frame.size.width)/2 ,600 , myLabel1.frame.size.width, myLabel1.frame.size.height);
            myLabel1.frame = newFrame_lable;
            CGRect newFrame_lable2 = CGRectMake((768 - myLabel2.frame.size.width)/2 ,630 , myLabel2.frame.size.width, myLabel2.frame.size.height);
            myLabel2.frame = newFrame_lable2;
            
            CGRect newFrame_Button1 = CGRectMake((768 - myButton1.frame.size.width)/2 ,654 , myButton1.frame.size.width, myButton1.frame.size.height);
            myButton1.frame = newFrame_Button1;
            myButton2.frame = newFrame_Button1;
        }
        else
        {
            CGRect newFrame = CGRectMake((703 - 440)/2 ,100 , 440, 440);
            myImageView.frame = newFrame;
            CGRect newFrame_lable = CGRectMake((703 - myLabel1.frame.size.width)/2 ,550 , myLabel1.frame.size.width, myLabel1.frame.size.height);
            myLabel1.frame = newFrame_lable;
            CGRect newFrame_lable2 = CGRectMake((703 - myLabel2.frame.size.width)/2 ,580 , myLabel2.frame.size.width, myLabel2.frame.size.height);
            myLabel2.frame = newFrame_lable2;
            CGRect newFrame_Button1 = CGRectMake((703 - myButton1.frame.size.width)/2 ,604 , myButton1.frame.size.width, myButton1.frame.size.height);
            myButton1.frame = newFrame_Button1;
            myButton2.frame = newFrame_Button1;
        }
    }
}
//___END_NGUYEN_HUY_SON__

- (void)setDelegatePlud:(id)new_delegate para:(NSArray *)para{
	Delegate = new_delegate;
    
    myArray = (NSDictionary*)para;
    
    NSString *myName = [myArray objectForKey:@"bild"];
    
    NSString *substring;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 
        substring = [PHOSPHON imagePost:myName imagePostfix:@"_g@2x"];
    }
    else
    {
        substring = [PHOSPHON imagePost:myName imagePostfix:@"_g"];
    }
    
    UIImage * img = [[UIImage imageNamed:substring] retain]; 
	[myImageView setImage: img];
    
    NSMutableString *tmpString1 = [[NSMutableString alloc] initWithString:[myArray objectForKey:@"artikel"]];
    
    if([tmpString1 length] > 0){
        if(![tmpString1 hasSuffix:@"'"]){
            [tmpString1 appendString:@" "];
        }
    }
    if([[myArray objectForKey:@"origFR"] length] !=0){
        [tmpString1 appendString:[myArray objectForKey:@"origFR"]];    
    } else {
        [tmpString1 appendString:[myArray objectForKey:@"bezeichnungFR"]];    
    }
    
        
    NSMutableString *tmpString2;
    if([[myArray objectForKey:@"origDE"] length] !=0){
        tmpString2 = [[NSMutableString alloc] initWithString:[myArray objectForKey:@"origDE"]];
    } else {
        tmpString2 = [[NSMutableString alloc] initWithString:[myArray objectForKey:@"bezeichnungDE"]];
    }

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 
        [myLabel1 setText:[[NSString alloc] initWithFormat:@"%d",[[NSUserDefaults standardUserDefaults] integerForKey:@"Integer"]]];
    }
    myLabel1.text = tmpString1;
    myLabel2.text = tmpString2;

    [tmpString1 release];
    [tmpString2 release];
    
    
    myButton1.hidden = false;
    myButton2.hidden = true;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 
        if([self isPadLandscape] == TRUE)
        {
            CGRect newFrame = CGRectMake((703 - 440)/2 ,100 , 440, 440);
            myImageView.frame = newFrame;
            CGRect newFrame_lable = CGRectMake((703 - myLabel1.frame.size.width)/2 ,550 , myLabel1.frame.size.width, myLabel1.frame.size.height);
            myLabel1.frame = newFrame_lable;
            CGRect newFrame_lable2 = CGRectMake((703 - myLabel2.frame.size.width)/2 ,580 , myLabel2.frame.size.width, myLabel2.frame.size.height);
            myLabel2.frame = newFrame_lable2;
            CGRect newFrame_Button1 = CGRectMake((703 - myButton1.frame.size.width)/2 ,604 , myButton1.frame.size.width, myButton1.frame.size.height);
            myButton1.frame = newFrame_Button1;
            myButton2.frame = newFrame_Button1;

        }
        else
        {
            CGRect newFrame = CGRectMake((768 - 440)/2, 164,440 ,  440);
            myImageView.frame = newFrame;
            CGRect newFrame_lable = CGRectMake((768 - myLabel1.frame.size.width)/2 ,600 , myLabel1.frame.size.width, myLabel1.frame.size.height);
            myLabel1.frame = newFrame_lable;
            CGRect newFrame_lable2 = CGRectMake((768 - myLabel2.frame.size.width)/2 ,630 , myLabel2.frame.size.width, myLabel2.frame.size.height);
            myLabel2.frame = newFrame_lable2;
            
            CGRect newFrame_Button1 = CGRectMake((768 - myButton1.frame.size.width)/2 ,654 , myButton1.frame.size.width, myButton1.frame.size.height);
            myButton1.frame = newFrame_Button1;
            myButton2.frame = newFrame_Button1;
        }
    }
    
}

- (IBAction) play:(id)sender{
	
    // first test
    NSString *myFilename;
    NSString *myName = [myArray objectForKey:@"ton"];
    
    if([myName hasSuffix:@".mp3"]){
        myFilename = [myName substringToIndex:[myName length]-4];
    } else {
        myFilename = [[NSString alloc] initWithString:myName];
    }
    
    //NSLog(@"--%@", myFilename);
    
    if(myName){
        NSURL *url = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:myFilename ofType:@"m4a"]];
        if(audioPlayer) {
            [audioPlayer stop];
            [audioPlayer release];
        }
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        audioPlayer.delegate = self;
        [audioPlayer play];
        [url release];
    }
    myButton1.hidden = true;
    myButton2.hidden = false;

}

- (IBAction) stop:(id)sender{
    
    if(audioPlayer) [audioPlayer stop];
    myButton1.hidden = false;
    myButton2.hidden = true;

}

- (void)audioPlayerDidFinishPlaying:IntroPlayer successfully: (BOOL)flag
{
    if(audioPlayer) [audioPlayer stop];
    myButton1.hidden = false;
    myButton2.hidden = true;
}
- (void)sstop{
    if(audioPlayer) [audioPlayer stop];
    myButton1.hidden = false;
    myButton2.hidden = true;
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    //___NGUYEN_HUY_SON
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 
        self = [super initWithNibName:@"cardview-iPad" bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
    }
    else{
        self = [super initWithNibName:@"CardViewController" bundle:nibBundleOrNil];
        if (self) {

        }
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    //__NGUYEN__HUY_SON
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    {
        CGFloat navBarHeight = 44.0f;    
        CGRect frame = CGRectMake(navigationBar.frame.origin.x, navigationBar.frame.origin.y, navigationBar.frame.size.width, navBarHeight);
        [navigationBar setFrame:frame];
        [navigationBar setBackgroundColor:[[UIColor alloc] initWithRed:134 green:166 blue:228 alpha:1.0]];

    }  //___END_NGUYEN_HUY_SON

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    {
        return YES;
    }
    else
    {
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
}
//______NGUYEN_HUY_SON___________________________________________________________________________________________
#pragma mark -
#pragma mark Managing the popover

- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
    
    [navigationBar.topItem setLeftBarButtonItem:barButtonItem animated:NO];

    CGRect newFrame = CGRectMake((768 - 440)/2, 164,440 ,  440);
    myImageView.frame = newFrame;
    CGRect newFrame_lable = CGRectMake((768 - myLabel1.frame.size.width)/2 ,600 , myLabel1.frame.size.width, myLabel1.frame.size.height);
    myLabel1.frame = newFrame_lable;
    CGRect newFrame_lable2 = CGRectMake((768 - myLabel2.frame.size.width)/2 ,630 , myLabel2.frame.size.width, myLabel2.frame.size.height);
    myLabel2.frame = newFrame_lable2;
    
    CGRect newFrame_Button1 = CGRectMake((768 - myButton1.frame.size.width)/2 ,654 , myButton1.frame.size.width, myButton1.frame.size.height);
    myButton1.frame = newFrame_Button1;
    myButton2.frame = newFrame_Button1;
}


- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
    
    // Remove the popover button from the toolbar.
    [navigationBar.topItem setLeftBarButtonItem:nil animated:NO];

    CGRect newFrame = CGRectMake((703 - 440)/2 ,100 , 440, 440);
    myImageView.frame = newFrame;
    CGRect newFrame_lable = CGRectMake((703 - myLabel1.frame.size.width)/2 ,550 , myLabel1.frame.size.width, myLabel1.frame.size.height);
    myLabel1.frame = newFrame_lable;
    CGRect newFrame_lable2 = CGRectMake((703 - myLabel2.frame.size.width)/2 ,580 , myLabel2.frame.size.width, myLabel2.frame.size.height);
    myLabel2.frame = newFrame_lable2;
    CGRect newFrame_Button1 = CGRectMake((703 - myButton1.frame.size.width)/2 ,604 , myButton1.frame.size.width, myButton1.frame.size.height);
    myButton1.frame = newFrame_Button1;
    myButton2.frame = newFrame_Button1;
}

//___END_NGUYEN_HUY_SON____________________________________________________________________________
@end
